<template>
  <div>
    <div class="header">
      <span ref="cancelBtn" v-show="this.query" class="home-back" @click="this.onCancel">
        <van-icon name="arrow-left" class="arrow"/>
      </span>
      <van-search
        v-model="value"
        :shape="this.query?'square':'round'"
        :background="this.query?'':'#4fc08d'"
        placeholder="请输入搜索关键词"
        :show-action="true"
        @focus="focus"
        @search="onSearch"
        @cancel="onCancel"
      >
        <template #action>
          <div v-if="query">
            <div @click="onSearch">搜索</div>
          </div>
          <van-icon
            v-else
            class="list-type-icon"
            :name="showCard?'location-o':'comment-o'"
            @click="changeType"
          />
        </template>
      </van-search>
    </div>
    <div class="item-table-head">
      <div class="item">默认</div>
      <div class="item">
        价格
        <img
          src=""
        >
      </div>
      <div class="item">
        销量
        <img
          src=""
        >
      </div>
      <div class="item">新品</div>
    </div>
    <div class="good-list" :class="{'on':showCard}">
      <a
        v-for="(product,index) in dataArr"
        :href="'/detail/'+product.id"
        :key="index"
        class="item"
        :title="product.storeName"
      >
        <div class="pictrue">
          <img :src="product.image" class="">
        </div>
        <div class="text">
          <div class="name line1">{{product.storeName}}</div>
          <div class="money font-color-red">
            ￥
            <span class="num">{{product.price}}</span>
          </div>
          <div class="vip acea-row row-between-wrapper">
            <div class="vip-money">￥{{product.vipPrice}}</div>
            <div>已售{{product.sales}}件</div>
          </div>
        </div>
      </a>
    </div>
    <SearchMain
      v-show="query"
      :list="keyList"
      :value="value"
      @vals="vals"
    />
  </div>
</template>

<script>
import SearchMain from "@/components/home/SearchMain";
import { vuexData, page } from "@/assets/js/mixin";

export default {
  name: "GoodList",
  mixins: [vuexData, page],
  components: {
    SearchMain
  },
  data() {
    return {
      showCard: true,
      value: "",
      query: false,
      keyList:[],

    };
  },
  methods: {
    focus() {
      this.query = true;
    },
    changeType() {
      this.showCard = !this.showCard;
    },
    onSearch() {
      console.log('this.value', this.value)
      this.search(this.value);
      this.query = false;
    },
    onCancel() {
      this.query = false;
    },
    vals(val) {
      this.value = val;
      this.query = false;
      this.search(val);
    },
    async search(value){
      const { data,code} =  await this.Api.searchList(value);
      if(code == "200"){
        this.setNewData(data.list);
        this.setTotal(data.total)
      }
    },
    async searchKey(key){
      const { data,code } =  await this.Api.searchKey(key);
      if(code == "200"){
        this.keyList = data;
      }
    }
  }
};
</script>
<style lang="less">
.header {
  background: #fff;
}
.home-back {
  float: left;
  line-height: 1.5;
  text-align: center;
  align-items: center;
}

.van-search__action {
  line-height: 0;
}
.list-type-icon {
  font-size: 0.5rem;
}
body {
  background: rgb(245, 245, 245);
}
.item-table-head {
  height: 0.86rem;
  color: #454545;
  position: fixed;
  left: 0;
  width: 100%;
  font-size: 0.38rem;
  background-color: #fff;
  top: 1.25rem;
  line-height: 2;
  z-index: 5;
  display: flex;
  .item {
    width: 25%;
    text-align: center;
    img {
      width: 0.25rem;
      height: 0.29rem;
      margin-left: 0.1rem;
    }
  }
}
.good-list {
  padding: 0 0.2rem;
  margin-top: 0.92rem;
  display: flex;
  align-items: center;
  /* -webkit-box-pack: justify; */
  -o-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  .item {
    width: 4.65rem;
    margin-top: 0.2rem;
    background-color: #fff;
    border-radius: 0.1rem;
    .pictrue {
      width: 100%;
      height: 4.45rem;
      img {
        width: 100%;
        height: 100%;
        border-radius: 0.1rem 0.1rem 0 0;
      }
    }
    .text {
      padding: 0.2rem 0.17rem 0.26rem 0.17rem;
      font-size: 0.3rem;
      color: #222;
      text-align: left;
    }
  }
  &.on {
    background-color: #fff;
    border-top: 1px solid #f6f6f6;
    padding: 0 0.2rem;
    margin-top: 0.72rem;
    display: block;
    align-items: center;
    justify-content: space-between;
    .item {
      width: 100%;
      display: -ms-flexbox;
      display: flex;
      display: -webkit-flex;
      display: -webkit-box;
      border-bottom: 1px solid #f6f6f6;
      padding: 0.3rem 0;
      margin: 0;
      .pictrue {
        width: 2.5rem;
        height: 2.5rem;
      }
      .text {
        width: 5.08rem;
        padding: 0 0 0 0.22rem;
      }
    }
  }
}
</style>

